﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.IO;
using System.Xml.Serialization;

namespace WhileAwayWpf
{
    public class UserInfo : INotifyPropertyChanged
    {
        private string _userName, _message, _photoLocation, _saveFolder;
        private string _lateMessage = "I'm late!", _noReturnTimeMessage = "Unknown";
        private DateTime _returnTime;
		private bool _photosEnabled;

        public UserInfo()
        {
            // Reasonable defaults
            _photoLocation = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
            _userName = Environment.UserName;
            _returnTime = DateTime.MinValue;
            
            _saveFolder = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "WhileAway");
            Filename = System.IO.Path.Combine(_saveFolder, "UserInfo.dat");
        }

        public string Filename { get; set; }
        public string UserName { get { return _userName; } set { _userName = value; PropChange("UserName"); } }
        public string LateMessage { get { return _lateMessage; } set { _lateMessage = value; PropChange("LateMessage"); } }
        public string NoReturnTimeMessage { get { return _noReturnTimeMessage; } set { _noReturnTimeMessage = value; PropChange("NoReturnTimeMessage"); } }
        public string PhotoLocation { get { return _photoLocation; } set { _photoLocation = value; PropChange("PhotoLocation"); } }
        public DateTime ReturnTime { get { return _returnTime; } set { _returnTime = value; PropChange("ReturnTime"); } }
        public string Message { get { return _message; } set { _message = value; PropChange("Message"); } }
		public bool PhotosEnabled { get { return _photosEnabled; } set { _photosEnabled = value; PropChange("PhotosEnabled"); } }
		public TimeSpan ReturnTimeRemaining
        {
            get
            {
                if (_returnTime == DateTime.MinValue)
                    return TimeSpan.MinValue;
                else
                    return _returnTime - DateTime.Now;
            }
            set { PropChange("ReturnTimeRemaining"); }
        }

        public void Load()
        {
            if (!File.Exists(Filename)) return;

            try
            {
                XmlSerializer ser = new XmlSerializer(typeof(UserInfo));
                using (StreamReader str = new StreamReader(Filename))
                {
                    UserInfo toLoad = (UserInfo)ser.Deserialize(str);
                    
                    this.Message = toLoad.Message;
                    this.PhotoLocation = toLoad.PhotoLocation;
                    this.PhotosEnabled = toLoad.PhotosEnabled;
                    this.ReturnTime = toLoad.ReturnTime;
                    this.UserName = toLoad.UserName;
                }
            }
            catch { }
        }

        public void Save()
        {
            Directory.CreateDirectory(_saveFolder);

            try
            {
                XmlSerializer ser = new XmlSerializer(typeof(UserInfo));
                using (StreamWriter str = new StreamWriter(Filename))
                {
                    ser.Serialize(str, this);
                }
            }
            catch { }
        }

		public void UpdateReturnTimeRemaining()
		{
			PropChange("ReturnTimeRemaining");
		}
		
        public event PropertyChangedEventHandler PropertyChanged;

        private void PropChange(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }

}